package com.vancouver.knows.business.tracker;

import java.util.Collections;
import java.util.List;

import com.pangu.bean.PageableResult;
import com.pangu.task.IResultReceiver;
import com.pangu.task.OperateResult;
import com.pangu.task.mark.ATaskMark;
import com.vancouver.knows.business.BusinessModule;
import com.vancouver.knows.business.BUtils.CategoryDiscountInfosSorter;
import com.vancouver.knows.business.bean.DiscountInfo;
import com.vancouver.knows.business.task.mark.FetchCityDiscountsCategoryTaskMark;

/**
 * 获取城事列表
 * 
 * @author chenxunlin01
 * 
 */
public class FetchCityDiscountsCategoryTracker extends ABInvokeTracker {

	public FetchCityDiscountsCategoryTracker(BusinessModule module, IResultReceiver resultReceiver) {
		super(module, resultReceiver);
	}

	@Override
	protected void handleTaskRefresh(ATaskMark taskMark, OperateResult result) {
		module.getCacheManager().getCityDiscountInfoCache().deleteItemInfoIndexFromCache(taskMark);
	}

	@Override
	public String TAG() {
		return FetchCityDiscountsCategoryTracker.class.getSimpleName();
	}

	@Override
	public void handleResult(OperateResult result) {
		PageableResult pageableResult = (PageableResult) result.getResultData();
		if (pageableResult.getContent() != null) {
			List<DiscountInfo> discountInfoCache = (List<DiscountInfo>) pageableResult.getContent();
			FetchCityDiscountsCategoryTaskMark aTaskMark = (FetchCityDiscountsCategoryTaskMark) result.getTaskMark();
			aTaskMark.setPageInfo(pageableResult.getPageInfo());
			List<DiscountInfo> discounts = module.getCacheManager().getCityDiscountInfoCache()
					.getItemInfoList(aTaskMark);
			discounts.addAll(discountInfoCache);
			Collections.sort(discounts, new CategoryDiscountInfosSorter());
			module.getCacheManager().getCityDiscountInfoCache().setItemInfoToCache(aTaskMark, discounts);
		}
	}
}
